package com.lagou.client;
import com.lagou.model.Exam;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.List;

/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {

    // 合成复用原则, 将cic 作为该类成员变量
    private ClientInitClose cic;

    /**
     * 通过构造方法实现成员变量的初始化
     */
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }
    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t  在线考试系统");
            System.out.println("--------------------------------");
            System.out.print("   [1]学员系统");
            System.out.println("     [2]管理员系统");
            System.out.println("   [0]退出系统");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose){
                case 1:
                    clientStudentLogin();   //选择学员登陆
                    break;
                case 2:
                    clientManagerLogin();    //选择管理员登陆
                    break;
                case 0:
                    System.out.println("正在退出系统");
                    quit();
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }
    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        //1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账号信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck", new User(username, password),null,null);

        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送管理员账户信息成功！");

        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("登陆成功，欢迎使用！");
            clientManagerSystem();   //登陆成功后，跳转到管理员系统模块
        }else{
            System.out.println("用户名或密码错误！");
        }
    }
    /**
     * 自定义成员方法实现客户端管理员系统界面的绘制
     */
    public void clientManagerSystem() throws IOException, ClassNotFoundException {
        while(true) {
            System.out.println("  \n\n\t\t  管理员系统");
            System.out.println("--------------------------------");
            System.out.print("   [1]学员管理");
            System.out.println("     [2]考题管理");
            System.out.println("   [0]后退");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    clientManageStudent();
                    break;
                case 2:
                    clientManageExam();
                    break;
                case 0:
                    System.out.println("正在后退");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }
    /**
     * 客户端界面的退出
     */
    public void quit() throws IOException {
        UserMessage tum = new UserMessage("quit", null,null,null);
        cic.getOos().writeObject(tum);
        System.out.println("退出");
    }


    /**
     * 自定义成员方法实现客户端学员管理界面的绘制
     */
    public void clientManageStudent() throws IOException, ClassNotFoundException {
        while(true){
            System.out.println("  \n\n\t\t  学生管理");
            System.out.println("--------------------------------");
            System.out.print("   [1]增加学生信息");
            System.out.println("     [2]删除学生信息");
            System.out.print("   [3]修改学生信息");
            System.out.println("     [4]查询学生信息");
            System.out.println("   [0]后退");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose){
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    modifyStudent();
                    break;
                case 4:
                    queryStudent();
                    break;
                case 0:
                    System.out.println("正在退出系统");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }

    }

    /**
     *自定义成员方法实现客户端考题管理界面的绘制
     */
    public void clientManageExam() throws IOException, ClassNotFoundException {
        while(true){
            System.out.println("  \n\n\t\t  考题管理");
            System.out.println("--------------------------------");
            System.out.print("   [1]增加考题信息");
            System.out.println("     [2]删除考题信息");
            System.out.print("   [3]修改考题信息");
            System.out.println("     [4]查询考题信息");
            System.out.println("   [0]后退");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose){
                case 1:
                    addExaminationQuestions();
                    break;
                case 2:
                    deleteExaminationQuestions();
                    break;
                case 3:
                    modifyExaminationQuestions();
                    break;
                case 4:
                    queryExaminationQuestions();
                    break;
                case 0:
                    System.out.println("正在退出");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }


    /**
     * 增加学生信息
     */
    public void addStudent() throws IOException, ClassNotFoundException {
        //1.准备学生的相关数据
        System.out.println("请输入学生的账号信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入学生的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("addStudent", new User(username, password),null,null);

        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送学生账户信息成功！");

        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("添加成功！");
        }else{
            System.out.println("用户名已存在！");
        }
    }

    /**
     *删除学生信息
     */
    public void deleteStudent() throws IOException, ClassNotFoundException {
        //1.准备学生的相关数据
        System.out.println("请输入学生的账号信息：");
        String username = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("deleteStudent", new User(username, null),null,null);
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送删除账户信息成功！");
        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("删除成功！");
        }else{
            System.out.println("用户名不存在！");
        }
    }

    /**
     *修改学生信息
     */
    public void modifyStudent() throws IOException, ClassNotFoundException {
        System.out.println("请输入学生的账号信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入修改后的学生的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("modifyStudent", new User(username, password),null,null);
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送学生账户信息成功！");
        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("修改成功！");
        }else{
            System.out.println("用户名不存在！");
        }
    }


    /**
     *查询学生信息
     */
    public void queryStudent() throws IOException, ClassNotFoundException {
        //1.准备学生的相关数据
        System.out.println("请输入学生的账号信息：");
        String username = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("queryStudent", new User(username, null),null,null);
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送查询账户信息成功！");
        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("查询成功！");
            System.out.println("账户信息："+tum.getUser().getUserName());
            System.out.println("密码信息："+tum.getUser().getPassword());
        }else{
            System.out.println("用户名不存在！");
        }
    }

    /**
     *增加考题信息
     */
    public void addExaminationQuestions() throws IOException, ClassNotFoundException {

        //1.准备考试题目的相关数据
        System.out.println("请输入考题题目");
        String title= ClientScanner.getScanner().next();
        System.out.println("请输入A选项：");
        String A = ClientScanner.getScanner().next();
        System.out.println("请输入B选项：");
        String B = ClientScanner.getScanner().next();
        System.out.println("请输入C选项：");
        String C = ClientScanner.getScanner().next();
        System.out.println("请输入D选项：");
        String D = ClientScanner.getScanner().next();
        System.out.println("请输入正确答案：");
        String Answer = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("addExam",null,new Exam(title,A,B,C,D,Answer),null);
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送考题信息成功！");
        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("添加成功！");
        }else{
            System.out.println("题目已存在！");
        }
    }
    /**
     *删除考题信息
     */
    public void deleteExaminationQuestions() throws IOException, ClassNotFoundException {

        //1.准备考试题目的相关数据
        System.out.println("请输入考题题目");
        String title= ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("deleteExam",null,new Exam(title,null,null,null,null,null),null);

        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送删除考题信息成功！");

        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("删除成功！");
        }else{
            System.out.println("题目不存在！");
        }
    }
    /**
     *修改考题信息
     */
    public void modifyExaminationQuestions() throws IOException, ClassNotFoundException {

        //1.准备考试题目的相关数据
        System.out.println("请输入考题题目");
        String title = ClientScanner.getScanner().next();
        System.out.println("请输入A选项：");
        String A = ClientScanner.getScanner().next();
        System.out.println("请输入B选项：");
        String B = ClientScanner.getScanner().next();
        System.out.println("请输入C选项：");
        String C = ClientScanner.getScanner().next();
        System.out.println("请输入D选项：");
        String D = ClientScanner.getScanner().next();
        System.out.println("请输入正确答案：");
        String Answer = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("modifyExam", null, new Exam(title, A, B, C, D, Answer),null);
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送修改考题信息成功！");
        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("修改成功！");
        } else {
            System.out.println("题目不存在！");
        }
    }


    /**
     *查询考题信息
     */
    public void queryExaminationQuestions() throws IOException, ClassNotFoundException {

        //1.准备考试题目的相关数据
        System.out.println("请输入考题题目");
        String title= ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("queryExam",null,new Exam(title,null,null,null,null,null),null);

        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送查询考题信息成功！");

        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("查询成功！");
            System.out.println("考题:"+tum.getExam().getTitle());
            System.out.println("    A."+tum.getExam().getA());
            System.out.println("    B."+tum.getExam().getB());
            System.out.println("    C."+tum.getExam().getC());
            System.out.println("    D."+tum.getExam().getD());
            System.out.println("正确答案是："+tum.getExam().getAnswer());
        }else{
            System.out.println("题目不存在！");
        }
    }
    /**
     *学员登录
     */
    public void clientStudentLogin() throws IOException, ClassNotFoundException {

        //1.准备学员登录的相关数据
        System.out.println("请输入学生的账号信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入学生的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("checkStudent", new User(username, password),null,null);

        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        System.out.println("客户端发送学生账户信息成功！");

        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("登陆成功，欢迎使用！");
            clientStudentSystem(tum);
        }else{
            System.out.println("用户名或密码错误！");
        }

    }

    /**
     *学员系统的界面绘制
     */
    public void clientStudentSystem(UserMessage tum) throws IOException, ClassNotFoundException {
        while(true){
            System.out.println("  \n\n\t\t  学员系统");
            System.out.println("--------------------------------");
            System.out.print("   [1]开始考试");
            System.out.println("     [2]查询成绩");
            System.out.print("   [3]修改密码");
            System.out.println("     [0]退出");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose) {
                case 1:
                    startExam(tum);
                    break;
                case 2:
                    queryScore(tum);
                    break;
                case 3:
                    modifyPassword(tum);
                    break;
                case 0:
                    System.out.println("正在退出");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
                    break;
            }
        }
    }

    /**
     *学员密码修改
     */
    public void modifyPassword(UserMessage tum) throws IOException, ClassNotFoundException {
        System.out.println("请输入修改的学生的新的密码信息：");
        String password1 = ClientScanner.getScanner().next();

        tum.getUser().setPassword(password1);
        tum.setType("modifyPassword");
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("密码修改成功");
        }else{
            System.out.println("密码修改失败");
        }
    }



    /**
     *开始考试
     */
    public void startExam(UserMessage tum) throws IOException, ClassNotFoundException {

        //1.开始考试模块,将传入学生对象的类型修改为 startExam
        tum.setType("startExam");
        System.out.println("学生开始考试！");

        //2.对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        cic.getOos().reset();

        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        List<Exam> exams = tum.getExams();
        if ("success".equals(tum.getType())){
            System.out.println("开始考试");
            for (Exam e:exams){
                System.out.println("题目："+e.getTitle());
                System.out.println("    A："+e.getA());
                System.out.println("    B："+e.getB());
                System.out.println("    C："+e.getC());
                System.out.println("    D："+e.getD());
                System.out.println("请输入正确答案：");
                String answer = ClientScanner.getScanner().next();
                e.setAnswer(answer);

            }
            tum.setExams(exams);
            System.out.println("提交请输入:提交");
            String answer1 = ClientScanner.getScanner().next();
            if ("提交".equals(answer1)){
                outScore(tum);
                System.out.println("您的作答已完毕, 考试结束");
            }else{
                System.out.println("输入错误");
            }
        }else{
            System.out.println("题库没有题！");
        }
    }



    /**
     *开始结束，导出分数
     */
    public void outScore(UserMessage tum) throws IOException, ClassNotFoundException {
        tum.setType("submitExam");
        System.out.println("客户端发送学生账户信息成功！");
        cic.getOos().writeObject(tum);
        cic.getOos().reset();
        //3.接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("此次考试的分数为："+tum.getUser().getScore());
        }else{
            System.out.println("用户名不存在！");
        }
    }
    /**
     *查询每次答题的分数
     */
    public void queryScore(UserMessage tum) throws IOException, ClassNotFoundException {
        tum.setType("queryScore");
        System.out.println("客户端发送学生账户信息成功！");
        cic.getOos().writeObject(tum);
        cic.getOos().reset();

        //接受服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            for(double s:tum.getUser().getScores()){
                System.out.println("考试的分数为："+s);
            }
        }else{
            System.out.println("用户名不存在！");
        }
    }
}
